
const app = getApp()  // 获取App实例
Page({
  /**
   * 页面的初始数据
   */
  data: {
    logo: app.getImgUrl('logo.png'),
    mobile: '',
    phone: '', password: "",
    showPass: false,
    logined: false,
    showCustomerPicker: false,
    currentCompany: {
      id: 0,
      corpid: '测',
      corpName: '测试企业名称这么长'
    },
    companyList: [],
    identityList: [],
    allindex: 0,//不管企业还是客户 下标
    currentIdentity: {},
    activeUserIndex: 0,//选择企业还是客户
    selectuse: {},
    selectindex: 0,//选择企业的下标
  },
  toggleShowPass() {
    this.setData({
      showPass: this.data.showPass ? false : true
    })
  },
  goforget() {
    wx.redirectTo({
      url: '/pages/forget/forget'
    })
  },
  // 选择企业
  toggleCheck(e) {

    const index = e.currentTarget.dataset.index; // 获取点击行的索引
    //判断选择的是企业还是员工




    const key = `identityList[${index}].chack`; // 构造动态 key
    this.data.identityList.map((item, indexs) => {
      const keys = `identityList[${indexs}].chack`; // 构造动态 key
      this.setData({
        [keys]: 'false'
      });
    })

    this.setData({
      [key]: this.data.identityList[index].chack == 'true' ? 'false' : 'true',
      allindex: index
    });
    if (this.data.identityList[index].status == '1') {
      //企业
      let status1list = this.data.identityList.filter(items => {
        return items.status == '1'
      })
      status1list.map((items, indexs) => {
        if (items.corpid == this.data.identityList[indexs].corpid) {
          this.setData({
            selectindex: indexs
          })
        }
      })
      this.setData({
        activeUserIndex: 0
      })
    } else {
      // 用户

      this.setData({
        activeUserIndex: 1
      })
    }
    this.setData({ currentCompany: this.data.identityList[index], showCustomerPicker: false })
  },
  goregister() {
    wx.redirectTo({
      url: '/pages/register/register'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.removeStorageSync('session');  // 删除 session 数据
    wx.removeStorageSync('userInfo'); // 删除 userInfo 数据
  },
  doLogin() {
    const _this = this
    if (!_this.data.phone || _this.data.phone.length < 4) {
      wx.showToast({
        title: '手机号格式不正确或为空',
        icon: 'error',
        duration: 1500
      })
    } else if (!_this.data.password || _this.data.password.length < 4) {
      wx.showToast({
        title: '密码太短或为空',
        icon: 'error',
        duration: 1500
      })
    } else {
      app.axios(
        'user',
        {
          op: "ajaxLoginUserInfoByPhone",
          password: app.MD5(_this.data.password), // MD5加密
          phone: _this.data.phone,
          ismd5: 1
        },
        false
      )
        .then(res => {
          if (res.item.length == 1) {
            //登录成功 且只有一个角色
            this.gopage(res.item[0])
            return
          }
          //员工
          let status1list = res.item.filter(items => {
            return items.status == '1'
          })
          // 客户
          let status2list = res.item.filter(items => {
            return items.status == '2'
          })
          console.log("status2list")
          console.log(status2list)
          if (res.item[0].status == '1') {
            _this.setData({
              activeUserIndex: 0
            })
          } else {
            _this.setData({
              activeUserIndex: 1
            })
          }
          let datas = res.item.map((items, index) => {
            if (index == 0) {
              items.chack = 'true'
            } else {
              items.chack = 'false'
            }
            return items
          })
          if (res.code) {
            _this.setData({
              logined: true,
              selectindex: 0,//选择企业下标
              isqiye: true,//是否选择的为企业  还是选择的是用户
              status1list: status1list,
              status2list: status2list,
              identityList: datas,
              currentIdentity: res.item[this.data.activeUserIndex]
            })
          }
        });
    }
  },
  chackqiye() {
    this.setData({
      showCustomerPicker: true
    })
  },
  handleCustomBack() {
    this.setData({
      logined: false
    })
  },
  confirmLogin() {
    // let userData = this.data.currentIdentity
    // 判断选择的是员工还是用户
    let items = {}
    this.data.identityList.map((item, indexs) => {
      if (item.chack == 'true') {
        items = item
      }
    })
    // 保存session到全局
    app.axios('user',
      {
        op: "ajaxUpLoginUserInfoSesion",
        corpid: items.corpid,
        userid: items.userid,
      },
      false
    )
      .then(res => {
        if (res.code) {
          this.gopage(items)
        }
      });
  },
  gopage(items) {
    //删除password字段
    delete items.password
    items.userType = this.data.activeUserIndex
    // 创建session对象
    const sessionData = {
      mkey: items.mkey,
      userid: items.userid,
      corpid: items.corpid,
      corpName: items.corpName,
      zzstatus: items.status
    };

    // 双重存储：内存 + 本地存储
    app.globalData.session = sessionData;
    wx.setStorageSync('session', sessionData);
    //储存用户信息
    wx.setStorageSync('userInfo', items);
    wx.showToast({
      title: '登录成功',
      icon: 'success',
      duration: 1500,
      success: () => {
        setTimeout(() => {
          app.navigateTo('/pages/index/index', 'switchTab')
        }, 1500);
      }
    })
  },
  changeActiveUser(e) {
    // 选择企业还是客户
    const dataset = e.currentTarget.dataset
    this.setData({
      activeUserIndex: dataset.index,
    })
    let arrays = this.data.identityList
    arrays = arrays.map((items, indexee) => {
      items.chack = 'false'
      return items
    })
    arrays = arrays.map((items, indexee) => {
      if (items.id == dataset.id) {
        items.chack = 'true'
        this.setData({
          allindex: indexee
        })
      }
      return items
    })
    this.setData({
      identityList: arrays
    })
  }


})